package pro.softzhang.algo.lc1600;

import java.util.Arrays;

public class LC1657_DetermineIfTwoStringsAreClose {
    public static void main(String[] args) {
        String word1 = "abc", word2 = "bca";
        System.out.println(new Solution().closeStrings(word1, word2));
    }

    /**
     *
     */
    static
    class Solution {
        public boolean closeStrings(String word1, String word2) {
            if(word1.length() != word2.length()) {
                return false;
            }

            int[] arr1 = new int[26], arr2 = new int[26];
            for(int i = 0; i < word1.length(); i++) {
                arr1[word1.charAt(i) - 'a']++;
                arr2[word2.charAt(i) - 'a']++;
            }

            for(int i = 0; i < arr1.length; i++) {
                if((arr1[i] == 0 && arr2[i] != 0) || (arr1[i] != 0 && arr2[i] == 0)) {
                    return false;
                }
            }

            Arrays.sort(arr1);
            Arrays.sort(arr2);
            for(int i = 0; i < arr1.length; i++) {
                if(arr1[i] != arr2[i]) {
                    return false;
                }
            }
            return true;
        }
    }
}
